package com.eam.handler;

import com.eam.data.EamSpareparts;
import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.openbravo.client.kernel.BaseActionHandler;
import org.openbravo.dal.service.OBDal;
import java.util.List;
import java.util.Map;

/**
 * 设备备品配件入库查询数据
 * 
 * @author Administrator
 * 
 */
public class SparepartAddsItemQueryHandler  extends BaseActionHandler {
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Override
	protected JSONObject execute(Map<String, Object> parameters, String data) {
		JSONObject jsonRep = new JSONObject();
		try {
			JSONObject jsonData = new JSONObject(data);
			String strId = jsonData.getString("ids");
			//	
			String[] ids = strId.split("#####");
			// 物料
			jsonRep.put("reqData", getCallPutMaterial(ids));
			
			
			//查询所有的供应商
			String rql ="select new map(id as id, name as gysname) from Emes_Vendor";


			List<Map<String, String>> vendorlist = OBDal.getInstance().getSession().createQuery(rql).list();
			JSONObject vendorMap = new JSONObject();
			for(Map<String, String> map : vendorlist) {
				vendorMap.put(map.get("id"), map.get("gysname"));
			}
			
			jsonRep.put("vendorMap", vendorMap);
			
			
			//查询所有的工序
			String wql ="select new map(id as id, name as gxname) from Emes_ProcessOpr";
		
			List<Map<String, String>> processlist = OBDal.getInstance().getSession().createQuery(wql).list();
			JSONObject processMap = new JSONObject();
			for(Map<String, String> map : processlist) {
				processMap.put(map.get("id"), map.get("gxname"));
			}
			
			jsonRep.put("processMap", processMap);
			
			//查询所有的车间
			String qql ="select new map(id as id, name as orgname) from Organization where organizationType.id = '1')";
		
			List<Map<String, String>> orglist = OBDal.getInstance().getSession().createQuery(qql).list();
			JSONObject orgMap = new JSONObject();
			for(Map<String, String> map : orglist) {
				orgMap.put(map.get("id"), map.get("orgname"));
			}
			
			jsonRep.put("orgMap", orgMap);
			
			
			//查询所有设备备品配件物料仓库下的储位
			String sql = "select new map(id as id, barcode as barcode) from Locator  where warehouse.id = 'FADC875A87544C9D80168957068700D1' ";
			List<Map<String, String>> list2 = OBDal.getInstance().getSession().createQuery(sql).list();
			JSONObject LocatorMap = new JSONObject();
			for(Map<String, String> map : list2) {
				LocatorMap.put(map.get("id"), map.get("barcode"));
			}
			
			jsonRep.put("LocatorMap", LocatorMap);
			

			jsonRep.put("result", true);
			OBDal.getInstance().commitAndClose();
		} catch (Exception e) {
			OBDal.getInstance().rollbackAndClose();
			e.printStackTrace();
			try {
				jsonRep.put("result", false);
				jsonRep.put("msg", "查询失败：" + e.getMessage());				
			} catch (Exception ex) {
			}
		}
		return jsonRep;
	}
	
	private JSONArray getCallPutMaterial(String[] ids) throws Exception {
		JSONArray data = new JSONArray();
		for (String id : ids) {
			EamSpareparts  Spare = OBDal.getInstance().get(EamSpareparts.class, id);
			JSONObject obj = new JSONObject();
			obj.put("ASN_CODE", Spare.getCode());											//物料编码
			obj.put("M_NAME", Spare.getName());												//物料名称
			obj.put("M_MODEL", Spare.getModel());											//规格型号
			
			String aql = "select ((select sum(jl.numbers)   "
					+ "from Eam_Spareparts_Collar jl "
					+ " left join Eam_Spareparts  wl on jl.Eam_Spareparts_id = wl.Eam_Spareparts_id "
					+ " where jl.type = 'added' and wl.code = '"+Spare.getCode()+"') -   (select NVL(sum(jl.numbers), 0)"
					+ "  from Eam_Spareparts_Collar jl "
					+ "  left join Eam_Spareparts  wl on jl.Eam_Spareparts_id = wl.Eam_Spareparts_id "
					+ "  where jl.type = 'Out' and wl.code = '"+Spare.getCode()+"')) as qty  from dual";
			
			
			Long numbers = (Long) OBDal.getInstance().getSession().createSQLQuery(aql).uniqueResult();
			
			obj.put("M_VENDOR", numbers);										//库存数量
			obj.put("Remaining", Spare.getSafetyStock());									//安全库存数量
			
			String companys = "";
			
			if( Spare.getCompany().equals("10")){
				companys= "对";
			}else if( Spare.getCompany().equals("20")){
				companys= "个";
			}else if( Spare.getCompany().equals("30")){
				companys= "根";
			}else if( Spare.getCompany().equals("40")){
				companys= "盒";
			}else if( Spare.getCompany().equals("50")){
				companys= "卷";
			}else if( Spare.getCompany().equals("60")){
				companys= "块";
			}else if( Spare.getCompany().equals("70")){
				companys= "片";
			}else if( Spare.getCompany().equals("80")){
				companys= "套";
			}
			if( Spare.getEmesProcessopr()  == null){
				
			}else{
				obj.put("processopr", Spare.getEmesProcessopr().getName());	
			}
			obj.put("M_USER", "");
			obj.put("prices","");
			obj.put("company",companys);											//单位
													//厂房区域
			obj.put("PlanIds", Spare.getId());												//行ID
			obj.put("remarks", "");
			data.put(obj);
		}
		return data;

	}

}
